// AppState.cs
// ------------------------------------------------------------------
//
// Description goes here....
//
// Author     : Dino
// Created    : Thu Oct 20 09:17:51 2011
// Last Saved : <2011-October-22 21:43:53>
//
// ------------------------------------------------------------------
//
// Copyright (c) 2011 by Dino Chiesa
// All rights reserved!
//
// ------------------------------------------------------------------

using System;
using System.Reflection;

namespace C4Tools.ML2
{

// [System.AttributeUsage(System.AttributeTargets.Property | System.AttributeTargets.Field)]
// public class AppStateItemAttribute : System.Attribute
// {
//     public string ValueName { get; set; }
//     public AppStateItemAttribute(string valueName)
//     {
//         this.ValueName = valueName;
//     }
//
//     public AppStateItemAttribute() { }
// }


    public sealed class AppState
    {
        public System.DateTime LastRun { get; set; }
        public int Runs { get; set; }
        public String LastControllerIp { get; set; }
    }

    #region Singleton AppState
    public sealed class AppStateManager
    {
        Microsoft.Win32.RegistryKey _appCuKey;
        private static readonly string _AppRegyPath = "Software\\Dino Chiesa\\MovieLister2";
        private readonly static AppStateManager _instance = new AppStateManager();
        public static AppStateManager Instance  { get { return _instance; } }
        static AppStateManager() { /* required for lazy init */ }

        Type t;
        System.Reflection.PropertyInfo[] pia;

        private AppStateManager()
        {
            this.t = typeof(AppState);
            this.pia = t.GetProperties();
        }


        public AppState GetState()
        {
            if (AppCuKey == null) return null;
            if ((pia == null) || (pia.Length == 0)) return null;

            AppState s = new AppState();

            foreach (PropertyInfo p in pia)
            {
                Type pt = p.PropertyType;
                if (pt == typeof(System.DateTime))
                {
                    String sv2 = (string)AppCuKey.GetValue(p.Name);
                    try
                    {
                        System.DateTime dt1 = System.DateTime.Parse(sv2);
                        p.SetValue(s, dt1, null);
                    }
                    catch (Exception) { /* ignore */ }
                }
                else if (pt == typeof(System.Int32))
                {
                    int x = (Int32)AppCuKey.GetValue(p.Name, 0);
                    p.SetValue(s, x, null);
                }
                else if (pt == typeof(System.String))
                {
                    String sv1 = (string) AppCuKey.GetValue(p.Name);
                    p.SetValue(s, sv1, null);
                }
            }

            return s;
        }


        public void StoreState(AppState s)
        {
            if (AppCuKey == null) return;
            if ((pia == null) || (pia.Length == 0)) return;

            foreach (PropertyInfo p in pia)
            {
                Type pt = p.PropertyType;
                if (pt == typeof(System.DateTime))
                {
                    DateTime dt1 = (p.Name == "LastRun") // special case
                        ? System.DateTime.Now
                        : (System.DateTime) p.GetValue(s, null);
                    AppCuKey.SetValue(p.Name, dt1.ToString("yyyy MMM dd HH:mm:ss"));
                }
                else if (pt == typeof(System.Int32))
                {
                    int x = (System.Int32) p.GetValue(s, null);
                    AppCuKey.SetValue(p.Name, x);
                }
                else if (pt == typeof(System.String))
                {
                    String sv1 = (string) p.GetValue(s, null);
                    AppCuKey.SetValue(p.Name, sv1);
                }
            }
        }

        public Microsoft.Win32.RegistryKey AppCuKey
        {
            get
            {
                if (_appCuKey == null)
                {
                    _appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_AppRegyPath, true);
                    if (_appCuKey == null)
                        _appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(_AppRegyPath);
                }
                return _appCuKey;
            }
            set { _appCuKey = null; }
        }

    }

    #endregion


}
